package com.study.hot.jetpack.demo_words.db;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import java.util.List;

/**
 * <pre>
 *     author : slx
 *     time   : 2020/03/30
 *     desc   :
 * </pre>
 */
public class WordsRepository {
    private static WordsRepository repository;
    private final WordsDao wordDao;
    private LiveData<List<Words>> allWordsLiveData;


    private WordsRepository(Context context) {
        WordsDatabase db = WordsDatabase.getInstance(context.getApplicationContext());
        wordDao = db.getWordDao();
        allWordsLiveData = wordDao.getAllWords();
    }

    public static WordsRepository newInstance(Context context) {
        if (repository == null) {
            synchronized (WordsRepository.class) {
                if (repository == null) {
                    repository = new WordsRepository(context);
                }
            }
        }
        return repository;
    }


    public void insertWords(Words... words) {
        new InsertAsyncTask(wordDao).execute(words);
    }

    public void deleteWords(Words... words) {
        new DeleteAsyncTask(wordDao).execute(words);
    }

    public void updateWords(Words... words) {
        new UpdateAsyncTask(wordDao).execute(words);
    }

    public void deleteAllWords() {
        new DeleteAllAsyncTask(wordDao).execute();
    }

    //查询全部
    public LiveData<List<Words>> getAllWords() {
        return this.allWordsLiveData;
    }

    //模糊查询
    public LiveData<List<Words>> getWordsByEnglish(String english) {
        return wordDao.getWordsByEnglish("%" + english + "%");
    }

    static class InsertAsyncTask extends AsyncTask<Words, Void, Void> {

        private WordsDao wordsDao;

        public InsertAsyncTask(WordsDao wordsDao) {
            this.wordsDao = wordsDao;
        }

        @Override protected Void doInBackground(Words... words) {
            wordsDao.insert(words);
            return null;
        }
    }

    static class DeleteAsyncTask extends AsyncTask<Words, Void, Void> {

        private WordsDao wordsDao;

        public DeleteAsyncTask(WordsDao wordsDao) {
            this.wordsDao = wordsDao;
        }

        @Override protected Void doInBackground(Words... words) {
            wordsDao.delete(words);
            return null;
        }
    }

    static class UpdateAsyncTask extends AsyncTask<Words, Void, Void> {

        private WordsDao wordsDao;

        public UpdateAsyncTask(WordsDao wordsDao) {
            this.wordsDao = wordsDao;
        }

        @Override protected Void doInBackground(Words... words) {
            wordsDao.update(words);
            return null;
        }
    }

    static class DeleteAllAsyncTask extends AsyncTask<Void, Void, Void> {

        private WordsDao wordsDao;

        public DeleteAllAsyncTask(WordsDao wordsDao) {
            this.wordsDao = wordsDao;
        }

        @Override protected Void doInBackground(Void... voids) {
            wordsDao.deleteAll();
            return null;
        }
    }

    static class GetAllAsyncTask extends AsyncTask<Void, Void, LiveData<List<Words>>> {

        private WordsDao wordsDao;

        public GetAllAsyncTask(WordsDao wordsDao) {
            this.wordsDao = wordsDao;
        }

        @Override protected LiveData<List<Words>> doInBackground(Void... voids) {
            return wordsDao.getAllWords();
        }
    }
}
